import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/core/api/user_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/page/home/mainpage.dart';
import 'package:FineArt/page/splash/role_choose.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/platform_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/utils/sputils.dart';
import 'package:flutter/material.dart';
import 'package:FineArt/constants.dart';

//类似广告启动页
class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    countDown();
  }

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    Size size = MediaQuery.of(context).size;
    return Scaffold(
        // appBar: AppBar(),
        body: Container(
            width: size.width,
            height: size.height,
            decoration: BoxDecoration(
              color: splash_bg_color,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  margin: EdgeInsets.fromLTRB(0, 50, 0, 0),
                  child: Image.asset(
                    "assets/images/icon_1.0.png",
                    width: getPortraitSWidth(458 / 1.5),
                    height: getPortraitSHeight(280 / 1.5),
                  ),
                ),
                SizedBox(height: size.height * 0.05),
                Text("全新版本",
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: getPortraitSHeight(32))),
                SizedBox(height: size.height * 0.08),
                Text("1.0版本，体验全新升级",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: getPortraitSHeight(18))),
                // SizedBox(height: size.height * 0.06),
                // InkWell(
                //   onTap: _goChoosePage,
                //   child: Container(
                //     margin: EdgeInsets.fromLTRB(0, 0, 0, 0),
                //     padding: EdgeInsets.fromLTRB(50, 5, 50, 5),
                //     child: Text("立即体验",
                //         style: TextStyle(
                //             fontSize: 18,
                //             color: Colors.white,
                //             decoration: TextDecoration.none)),
                //     decoration: BoxDecoration(
                //         border: Border.all(width: 1, color: Colors.white),
                //         borderRadius: BorderRadius.all(Radius.circular(15))),
                //   ),
                // ),
              ],
            )
            // ]),
            ));
  }

  void _handleLogin(response) {
    bool succ = false;
    if (response.code == 200) {
      if (response.token != null) {
        succ = true;
        SPUtils.saveToken("Bearer " + response.token);
        //登录成功
        _goMainPage();
      }
    }
    if (!succ) {
      _goChoosePage();
    }
  }

  _goMainPage() {
    ModuleApi().modules().then((res) {
      if (res.code == 200) {
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) {
          return TabMainPage("", res.data);
        }));
      } else {
        ToastUtils.toast(res.msg);
      }
    });
  }

  _login() async {
    String name = SPUtils.getUsername();
    String uuid = await PlatformUtil.uuid(name);
    String ver = await PlatformUtil.getVersion();
    String pass = SPUtils.getPassword();
    String role = SPUtils.getRole();

    if (name != null && pass != null && role != null) {
      LoginBody body = new LoginBody(name, pass, uuid, 301,ver);
      if (role == 'student') {
        UserApi().studentLogin(body).then((response) {
          _handleLogin(response);
        }).catchError((onError) {
          _goChoosePage();
        });
      } else {
        UserApi().teacherLogin(body).then((response) {
          _handleLogin(response);
        }).catchError((onError) {
          _goChoosePage();
        });
      }
    } else {
      _goChoosePage();
    }
  }

  //倒计时
  void countDown() {
    LogUtil.d("count down");
    var _duration = Duration(seconds: 3);

    new Future.delayed(_duration,  _login);
  }

  //页面跳转
  void _goChoosePage() {
    // XRouter.replace(Routes.mainHomePage);
    Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) {
      return ChooseRole();
    }));
  }
}
